// pages/login/login.js
const util = require('../../utils/util.js')

const config = require('../../utils/config.js')

//获取应用实例
const app = getApp()
Page({
  data: {
    data: [],
    meta: {},
    status_count: {
      total_count: 2474,
      consumption: "1737.95",
      normal_upload_rate: "10.75%",
    },
    value: "",
    meter_number:'',
    page: 1,
    per_page: 10,
    management_companies:app.globalData.userInfo.management_companies,
    index: 0,
  },
  onLoad: function (options) {

    this.fecth({
      page: 1,

    })


  },
  bindPickerChange: function (e) {
    const that = this
    console.log('picker发送选择改变，索引值为', e.detail.value)
    console.log("选中的id值:" + that.data.management_companies[e.detail.value].id)
    console.log("选中的company_name值:" + that.data.management_companies[e.detail.value].name)
    console.log(e);
    this.setData({
      index: e.detail.value,
    }, function () {
      this.fecth({
        page: 1,

      })
    })

  },
  bindtapLocation: function (e) {
    const ds = e.currentTarget.dataset;
    console.log('ds',ds)
    if(Number(ds.item.latitude)===0){
      console.log('暂未有定位')
      wx.showToast({
        title: '暂未有定位',
        icon: 'none'
      })
      return
    }
    wx.openLocation({
      latitude: Number(ds.item.latitude), // 纬度，范围为-90~90，负数表示南纬
      longitude:Number(ds.item.longitude), // 经度，范围为-180~180，负数表示西经
      scale: 14, // 缩放比例
    })
  },
  bindtapDetail: function (e) {
    const ds = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/list_detail/detail?number=${ds.item.number}&id=${ds.item.id}`
    })
  },
  bindinput(e) {
    this.setData({
      value: e.detail.value
    })
  },
  bindinputMeterNumber(e) {
    this.setData({
      meter_number: e.detail.value
    })
  },
  fecth: function (params) {
    wx.stopPullDownRefresh();
    const that = this;
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    if (!app.globalData.userInfo) {
      wx.reLaunch({
        url: '/pages/login/login'
      })
      return false;
    }
    wx.request({
      url: `${config.prefix}/concentrators`,
      header: {
        Authorization: `Bearer ${app.globalData.userInfo.token}`
      },
      method: 'GET',
      data: {
        per_page: 20,
        number: this.data.value,
        meter_number: this.data.meter_number,
        order_direction: 'desc',
        company_id: that.data.management_companies[that.data.index].id,
        ...params
      },
      success: function (res) {
        console.log('get res', res)
        setTimeout(function () {
          wx.hideLoading()
        }, 100)
        if (res.statusCode === 200) {
          for(let i=0;i<res.data.data.length;i++){
            res.data.data[i]={
              ...res.data.data[i],
              platform:res.data.data[i].config.platform,
              meter_count:res.data.data[i].config.meter_config?Object.keys(res.data.data[i].config.meter_config).length:'',
              version:(res.data.data[i].concentrator_info&&res.data.data[i].concentrator_info.main_software_version)?`${res.data.data[i].concentrator_info.main_software_version}.${res.data.data[i].concentrator_info.sub_software_version}`:''
            }
          }
          console.log('获取成功');
          if (params.page === 1) {
            that.setData({
              data: res.data.data,
              meta: res.data.meta,
              page: params.page,

            })
          } else {

            let pushData = that.data.data.concat(res.data.data)

            that.setData({
              data: pushData,
              meta: res.data.meta,
              page: params.page
            })
          }


        } else {
          util.converErrorCodeToMsg(res)
        }
      }


    })
  },

  handleTapItem: function (e) {
    console.log(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: `/pages/issue_detail/detail?number=${e.currentTarget.dataset.item.number}&id=${e.currentTarget.dataset.item.id}`
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh')
    this.fecth({
      page: 1,

    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.fecth({
      page: 1,

    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('onPullDownRefresh')
    console.log(this.data)
    if (this.data.page >= this.data.meta.last_page) {
      // wx.showToast({
      //   title: '没有更多数据',
      //   icon: 'none'
      // })
      return false;
    }
    this.fecth({
      page: this.data.page + 1,

    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})